package cn.iocoder.yudao.module.alipay.controller.admin.infoproduction;

import cn.iocoder.yudao.module.alipay.controller.admin.infoslideshow.vo.InfoSlideshowRespVO;
import cn.iocoder.yudao.module.alipay.convert.infoslideshow.InfoSlideshowConvert;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoslideshow.InfoSlideshowDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.infoproduction.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoproduction.InfoProductionDO;
import cn.iocoder.yudao.module.alipay.convert.infoproduction.InfoProductionConvert;
import cn.iocoder.yudao.module.alipay.service.infoproduction.InfoProductionService;

@Tag(name = "管理后台 - 作品")
@RestController
@RequestMapping("/alipay/info-production")
@Validated
public class InfoProductionController {

    @Resource
    private InfoProductionService infoProductionService;

    @PostMapping("/create")
    @Operation(summary = "创建作品")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:create')")
    public CommonResult<Long> createInfoProduction(@Valid @RequestBody InfoProductionCreateReqVO createReqVO) {
        return success(infoProductionService.createInfoProduction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作品")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:update')")
    public CommonResult<Boolean> updateInfoProduction(@Valid @RequestBody InfoProductionUpdateReqVO updateReqVO) {
        infoProductionService.updateInfoProduction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:info-production:delete')")
    public CommonResult<Boolean> deleteInfoProduction(@RequestParam("id") Long id) {
        infoProductionService.deleteInfoProduction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:query')")
    public CommonResult<InfoProductionRespVO> getInfoProduction(@RequestParam("id") Long id) {
        InfoProductionDO infoProduction = infoProductionService.getInfoProduction(id);
        return success(InfoProductionConvert.INSTANCE.convert(infoProduction));
    }

    @GetMapping("/list")
    @Operation(summary = "获得作品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:query')")
    public CommonResult<List<InfoProductionRespVO>> getInfoProductionList(@RequestParam("ids") Collection<Long> ids) {
        List<InfoProductionDO> list = infoProductionService.getInfoProductionList(ids);
        return success(InfoProductionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getListByTypeId")
    @Operation(summary = "根据分类id获取作品集")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:query')")
    public CommonResult<List<InfoProductionRespVO>> getListByTypeId(String typeId) {
        List<InfoProductionDO> list = infoProductionService.getListByTypeId(typeId);
        return success(InfoProductionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得作品分页")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:query')")
    public CommonResult<PageResult<InfoProductionRespVO>> getInfoProductionPage(@Valid InfoProductionPageReqVO pageVO) {
        PageResult<InfoProductionDO> pageResult = infoProductionService.getInfoProductionPage(pageVO);
        return success(InfoProductionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作品 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:info-production:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoProductionExcel(@Valid InfoProductionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InfoProductionDO> list = infoProductionService.getInfoProductionList(exportReqVO);
        // 导出 Excel
        List<InfoProductionExcelVO> datas = InfoProductionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "作品.xls", "数据", InfoProductionExcelVO.class, datas);
    }

}
